# -*- coding:utf-8 -*-

import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow,QLabel,QFrame,QPushButton,QAction,QMessageBox,QInputDialog
from PyQt5.QtGui import QIcon, QFont
from .word_memory import get_new_word,get_example,collect,play,insert_remember_record
from .record_window import RecordTableWidget
from .collection_window import CollectionTableWidget
import global_variable

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.set_ui()
        # self.menu_init()
        self.word_ui_init()
        self.button_init()



    def show(self) -> None:
        super(Main, self).show()
        self.word_init()


    def set_ui(self):
        self.setWindowTitle('Main page')
        self.setWindowIcon(QIcon('./IMG/python-logo.png'))
        self.setFixedSize(1000, 800)
        self.setFont(QFont('Consolas'))
        # self.setStyleSheet("background-image: url('./IMG/welcome.jpg'); background-repeat: no repeat")
        # self.setWindowOpacity(0.9)

    def word_init(self):
        word_tuple = get_new_word()
        example_list = get_example()
        while word_tuple is None:
            word_tuple = get_new_word()
            example_list = get_example()
        self.spelling_label.setText(word_tuple[1])
        self.pronounce_label.setText("en."+word_tuple[2]+"   uk."+word_tuple[3])
        self.meaning_label.setText(word_tuple[4])


        example = " "
        count = len(example_list)+1
        for example_tuple in example_list:
            count= count-1
            example = str(count)+'.'+example_tuple[2]+example_tuple[3]+'\n'+example

        self.example_label.setText(example)
        self.frequency_label.setText(" ")

    def word_ui_init(self):
        # 设置边框样式 可选样式有Box Panel等
        # temp_label.setFrameShape(QtWidgets.QFrame.Box)
        # # 设置阴影 只有加了这步才能设置边框颜色
        # # 可选样式有Raised、Sunken、Plain（这个无法设置颜色）等
        # temp_label.setFrameShadow(QtWidgets.QFrame.Raised)
        # # 设置线条宽度
        # temp_label.setLineWidth(5)
        # # 设置背景颜色，包括边框颜色
        # temp_label.setStyleSheet('background-color: rgb(255, 0, 0)')



        self.spelling_label = QLabel('receptive', self)
        self.pronounce_label = QLabel('rɪˈseptɪv', self)
        self.meaning_label = QLabel('"善于接受的"', self)
        self.example_label = QLabel(r'  ex: The voters had seemed receptive to his ideas...,选民看上去很愿意接受他的想法。  ex: The voters had seemed receptive to his ideas...,选民看上去很愿意接受他的想法。  ex: The voters had seemed receptive to his ideas...,选民看上去很愿意接受他的想法。  ex: The voters had seemed receptive to his ideas...,选民看上去很愿意接受他的想法。  ex: The voters had seemed receptive to his ideas...,选民看上去很愿意接受他的想法。', self)
        self.example_label.setWordWrap(True)
        self.frequency_label = QLabel('频率 *****', self)
        # 设置边框样式 可选样式有Box Panel等
        self.spelling_label.setFrameShape(QFrame.Box)
        # 可选样式有Raised、Sunken、Plain（这个无法设置颜色）等
        # self.spelling_label.setFrameShadow(QFrame.Raised)
        # 设置线条宽度
        self.spelling_label.setLineWidth(5)
        # self.spelling_label.setStyleSheet('background-color: rgb(255, 0, 0)')

        self.spelling_label.move(50, 100)
        self.spelling_label.resize(600, 160)
        self.spelling_label.setFont(QFont("Roman times", 50))

        self.pronounce_label.move(50, 270)
        self.pronounce_label.resize(500, 60)
        self.pronounce_label.setFont(QFont("Roman times",20))
        self.pronounce_label.setFrameShape(QFrame.Box)
        self.pronounce_label.setFrameShadow(QFrame.Raised)
        self.pronounce_label.setLineWidth(1)

        self.meaning_label.move(50, 340)
        self.meaning_label.setWordWrap(True)  # Lable的换行
        self.meaning_label.setFont(QFont("Roman times", 20))
        self.meaning_label.resize(600, 100)
        self.meaning_label.setFrameShape(QFrame.Box)
        self.meaning_label.setFrameShadow(QFrame.Raised)
        self.meaning_label.setLineWidth(1)


        self.example_label.move(50, 460)
        self.example_label.setWordWrap(True)  # Lable的换行
        self.example_label.setFont(QFont("Roman times", 15))
        self.example_label.resize(600, 300)
        self.example_label.setFrameShape(QFrame.Box)
        self.example_label.setFrameShadow(QFrame.Raised)
        self.example_label.setLineWidth(1)

        self.frequency_label.move(50, 400)
        self.frequency_label.setWordWrap(True)  # Lable的换行
        self.frequency_label.setFont(QFont("Roman times", 10))
        self.frequency_label.resize(300, 300)
    def menu_init(self):
        menu_font = QFont()
        menu_font.setFamily('Arial')
        menu_font.setPixelSize(40)
        self.user_menu = self.menuBar().addMenu('&账户 ')
        self.select_menu = self.menuBar().addMenu('&单词量检测')
        self.collection_menu = self.menuBar().addMenu('&单词收藏本 ')
        self.record_menu = self.menuBar().addMenu('&记忆记录 ')

        show_action = QAction()
        show_action.triggered.connect(self.get_record_windows)
        self.record_menu.addAction(show_action)

        # self.menuBar().setFixedSize(10,10)
        # self.menuBar().setFixedSize(800,40)
        self.menuBar().setFont(menu_font)
        # self.select_menu.addAction(self.select_vocabulary())

    def button_init(self):
        self.play_button = QPushButton('播放', self)
        self.collect_button = QPushButton('收藏',self)
        self.spell_button = QPushButton("拼写",self)
        self.next_word_button = QPushButton("下一个", self)
        self.next_word_button.clicked.connect(self.next_word_button_click)
        self.collect_button.clicked.connect(self.collect_button_click)
        self.play_button.clicked.connect(self.play_audio)

        self.spell_button.clicked.connect(self.spell)

        self.play_button.resize(100, 60)
        self.play_button.move(550, 270)

        self.collect_button.move(700,100)
        self.collect_button.resize(150,150)

        self.next_word_button.move(700,300)
        self.next_word_button.resize(150,150)

        self.spell_button.move(700,500)
        self.spell_button.resize(150,150)

        self.record_button = QPushButton('记录', self)
        self.collections_button = QPushButton('单词收藏本',self)
        self.detect_button = QPushButton("单词量检测",self)
        self.detect_button.move(0,0)
        self.detect_button.resize(100,50)

        self.record_button.move(100,0)
        self.record_button.resize(100,50)
        self.record_button.clicked.connect(self.get_record_windows)

        self.collections_button.move(200,0)
        self.collections_button.resize(100,50)
        self.collections_button.clicked.connect(self.get_collection_window)



    def next_word_button_click(self):
        #讲当前单词插入record
        insert_remember_record()
        #
        self.word_init()

    def get_record_windows(self):
        self.record_window = RecordTableWidget()
        self.record_window.show()

    def collect_button_click(self):
        collect()

    def get_collection_window(self):
        self.collection_window = CollectionTableWidget()
        self.collection_window.show()

    def play_audio(self):
        play()

    def spell(self):
        self.spelling_label.setText("")
        word, ok = QInputDialog.getText(self, 'Spell', 'Please enter the word:')

        if ok:
            if word == global_variable.get("v_name"):
                self.spelling_label.setText(word)
                print("   qweqwe")

                # self.name_line.setText(name)
    def closeEvent(self, event):
        reply = QMessageBox.question(self,'本程序',"是否要退出程序？",QMessageBox.Yes | QMessageBox.No,QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

# https://ssl.gstatic.com/dictionary/static/sounds/oxford/good--_gb_1.mp3

    # def
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())
